<?php
namespace app\admin\model;

use app\commen\model\Group;
use think\Model;

class MemberModel extends Model
{
    protected $name               = 'member';
    protected $autoWriteTimestamp = true;   // 开启自动写入时间戳
    //性别获取器
    public function getSexAttr($value)
    {
        $status = [0=>'未知',1=>'男',2=>'女'];
        return $status[$value];
    }
    public function setSexAttr($value)
    {
        if($value == '男'){
            return 1;
        }elseif($value == '女'){
            return 2;
        }else{
            return 0;
        }
    }
    public function getCreateTimeAttr($time)
    {
        return $time;
    }
    public function getUpdateTimeAttr($time)
    {
        return $time;
    }
    //密码修改器
    public function setPwdAttr($value){
        //MD5加密
        return md5($value);
    }
    //更新
    public static function update_data($data){
        // 过滤post数组中的非数据表字段数据
        $is_cunzai = self::phone_email_verify($data['phone'],$data['email'],$data['id']);

        if($is_cunzai){
           return false;
        }
        $member = new MemberModel();
        $data['update_time'] = date('Y-m-d H:i:s',time());
        $re = $member->allowField(true)->save($data,['id' => $data['id']]);
        return $re;
    }
    //验证手机号 邮箱是否重复
    protected static function phone_email_verify($phone='',$email='',$id = 0){

        $re = MemberModel::where('id','<>',$id)->where('phone',$phone)->where('email',$email)->select();

        return $re;
    }
    //登录验证
    public static function login_verify($account ,$pwd){
        //验证是否为手机号登录
        $re = MemberModel::where('phone',$account)->where('pwd',md5($pwd))->find();
        if($re) return $re;
        //验证是否为邮箱登录
        $re = MemberModel::where('email',$account)->where('pwd',md5($pwd))->find();
        if($re) return $re;
        return false;
    }
    //添加
    public static function create_data($data)
    {
        $is_cunzai = self::phone_email_verify($data['phone'],$data['email']);
        if($is_cunzai) return false;
        $data['create_time'] = date('Y-m-d H:i:s',time());
        $data['update_time'] = date('Y-m-d H:i:s',time());
        $member = new MemberModel();
        $re = $member->allowField(true)->save($data);
        if($re){
            return $member->id;
        }else{
            return false;
        }
    }
    //更新
    public static function uodata_data($data){
        $is_cunzai = self::phone_email_verify($data['phone'],$data['email']);
        if($is_cunzai) return false;
        $data['update_time'] = date('Y-m-d H:i:s',time());
        $member = new MemberModel();
        $re = $member->allowField(true)->save($data,['id'=>$data['id']]);
        if($re){
            return $member->id;
        }else{
            return false;
        }
    }

}